package dev.huidocs.docs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.coadmin.utils.QueryHelpMybatisPlus;
import com.gitee.coadmin.base.PageInfo;
import com.gitee.coadmin.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import dev.huidocs.docs.domain.DocResHistory;
import dev.huidocs.docs.service.DocResHistoryService;
import dev.huidocs.docs.service.dto.DocResHistoryDTO;
import dev.huidocs.docs.service.dto.DocResHistoryQueryParam;
import dev.huidocs.docs.service.mapper.DocResHistoryMapper;
import dev.huidocs.docs.service.converter.DocResHistoryConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
 * @author jinjin
 * @since 2024-10-11
 */
@Service
@RequiredArgsConstructor
// @CacheConfig(cacheNames = DocResHistoryService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DocResHistoryServiceImpl implements DocResHistoryService {

    // private final RedisUtils redisUtils;
    private final DocResHistoryMapper docResHistoryMapper;
    private final DocResHistoryConverter docResHistoryConverter;

    @Override
    public PageInfo<DocResHistoryDTO> pageByParam(DocResHistoryQueryParam query, Pageable pageable) {
        IPage<DocResHistory> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DocResHistory> page = docResHistoryMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return docResHistoryConverter.convertPage(page);
    }

    @Override
    public List<DocResHistoryDTO> listByParam(DocResHistoryQueryParam query){
        return docResHistoryConverter.toDto(docResHistoryMapper.selectList(QueryHelpMybatisPlus.getPredicate(query, "id", false)));
    }

    @Override
    public long countByParam(DocResHistoryQueryParam query) {
        return docResHistoryMapper.selectCount(QueryHelpMybatisPlus.getPredicate(query));
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DocResHistoryDTO getById(Long id) {
        if (id == null) {
            return null;
        }
        return docResHistoryConverter.toDto(docResHistoryMapper.selectById(id));
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void insert(DocResHistoryDTO res) {
        DocResHistory entity = docResHistoryConverter.toEntity(res);
        docResHistoryMapper.insert(entity);
        res.setId(entity.getId());
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DocResHistoryDTO res){
        DocResHistory entity = docResHistoryConverter.toEntity(res);
        docResHistoryMapper.updateById(entity);
        // delCaches(res.id);
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(Set<Long> ids){
        // delCaches(ids);
        docResHistoryMapper.deleteBatchIds(ids);
    }
    
    /*
    private void delCaches(Long id) {
        redisUtils.del(CACHE_KEY + "::id:" + id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/
}
